package edu.ccut.saturn.studio.wfg.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.model.ShapesDiagram;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnInformationNode;

public class ActivitySelectionDialog extends TitleAreaDialog{

	private TableViewer tableViewer;
	
	private Table table;
	
	private Shape shape;
	
	private List<Shape> shapes = new ArrayList<Shape>();
	
	private String obtainFromActivityText = "";
	
	public ActivitySelectionDialog(Shell parentShell, Shape shape) {
		super(parentShell);
		this.shape = shape;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
		newShell.setText(Messages.getString("ActivitySelectionDialog.chooesActivitor"));   //$NON-NLS-1$
	}
	
	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("ActivitySelectionDialog.ChooesActionFromList"));   //$NON-NLS-1$
		return contents;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Control createDialogArea(Composite parent) {
		
		Composite area = (Composite) super.createDialogArea(parent);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		final Composite composite = new Composite(area, SWT.NONE);
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true);
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		composite.setLayout(gridLayout);

		tableViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
		tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(final SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
				if(selection!=null){
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		table = tableViewer.getTable();
		table.setLinesVisible(false);
		table.setHeaderVisible(false);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		tableViewer.setContentProvider(new TableViewerContentProvider());
		tableViewer.setLabelProvider(new ActivitySelectionLabelProvider());

		final TableColumn imageTableColumn = new TableColumn(table, SWT.NONE);
		imageTableColumn.setWidth(25);
		imageTableColumn.setText(Messages.getString("ActivitySelectionDialog.Marker"));   //$NON-NLS-1$

		final TableColumn identifyTableColumn = new TableColumn(table, SWT.NONE);
		identifyTableColumn.setWidth(300);
		identifyTableColumn.setText(Messages.getString("ActivitySelectionDialog.View"));   //$NON-NLS-1$
		
		tableViewer.setInput(copyShapesArray(obtainShapes()));
		
		return area;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(400,360);
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if(buttonId == IDialogConstants.OK_ID){
			IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
			Shape tshn = (Shape)selection.getFirstElement();
			setObtainFromActivityText(tshn.getName() + "[" + tshn.getId() + "]");
		}
		shapes.add(shape);
		super.buttonPressed(buttonId);
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}
	
	/**
	 * 得到白板下所有的模型(不包括自己)
	 * 
	 * */
	@SuppressWarnings("unused")
	private List<Shape> obtainShapes(){
		shapes = ((ShapesDiagram)shape.getParent()).getShapes();
		shapes.remove(shape);
		return shapes;
	}
	
	/**
	 * 复制shapes数组；
	 * 得到除SaturnInformationNode节点和本节点外的白板上的所有节点
	 * 
	 * */
	private List<Shape> copyShapesArray(List<Shape> shapes){
		List<Shape> tempList = new ArrayList<Shape>();
		for(Shape shape: shapes){
			tempList.add(shape.cloneShape(shape));
		}
		
		for(Shape shape: tempList){
			if(shape instanceof SaturnInformationNode){
				tempList.remove(shape);
				return tempList;
			}
		}
		
		return tempList;
	}
	
	public String getObtainFromActivityText() {
		return obtainFromActivityText;
	}

	public void setObtainFromActivityText(String obtainFromActivity) {
		this.obtainFromActivityText = obtainFromActivity;
	}
}